
#if !defined(__CLIENT_VERSION_FRAME_H__)
#define __CLIENT_VERSION_FRAME_H__

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "FlatTabCtrl.h"
#include "ExListViewCtrl.h"
#include "../client/ClientVersionManager.h"
#include "../client/File.h"
#include "../client/UpdateManager.h"

#define SERVER_MESSAGE_MAP 7

class ClientVersionFrame : public MDITabChildWindowImpl<ClientVersionFrame>, public StaticFrame<ClientVersionFrame,ResourceManager::CLIENT_VERSIONS>
	, private VersionManagerListener, private UpdateManagerListener
{
public:

	// Base class typedef
	typedef MDITabChildWindowImpl<ClientVersionFrame> baseClass;

	// Constructor/destructor
	ClientVersionFrame() : closed(false) {
		ClientVersionManager::getInstance()->addListener(this);
		UpdateManager::getInstance()->addListener(this);
	}
	virtual ~ClientVersionFrame() { 
		ClientVersionManager::getInstance()->removeListener(this);
		UpdateManager::getInstance()->removeListener(this);
	}

	// Frame window declaration
	DECLARE_FRAME_WND_CLASS_EX(_T("ClientVersionFrame"), IDR_ADLSEARCH, 0, COLOR_3DFACE);

	// Inline message map
	BEGIN_MSG_MAP(ClientVersionFrame)
		MESSAGE_HANDLER(WM_CREATE, onCreate)
		MESSAGE_HANDLER(WM_CLOSE, onClose)
		COMMAND_ID_HANDLER(IDC_ADD, onAddClient)
		COMMAND_ID_HANDLER(IDC_EDIT, onChangeClient)
		COMMAND_ID_HANDLER(IDC_REMOVE, onRemoveClient)
		COMMAND_ID_HANDLER(IDC_UPDATE_CHECK, onContextAction)
		COMMAND_ID_HANDLER(IDC_SITE, onContextAction)
		COMMAND_ID_HANDLER(IDC_XML, onContextAction)
		COMMAND_ID_HANDLER(IDC_RELOAD_CLIENTS, onReload)
		NOTIFY_HANDLER(IDC_C_VER_LIST, NM_DBLCLK, onDblClick)
		NOTIFY_HANDLER(IDC_C_VER_LIST, NM_CUSTOMDRAW, onCustomDraw)
		MESSAGE_HANDLER(WM_CONTEXTMENU, onContextMenu)
		CHAIN_MSG_MAP(baseClass)
	END_MSG_MAP()

	// Message handlers
	LRESULT onCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
	LRESULT onClose(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled);
	LRESULT onAddClient(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
	LRESULT onChangeClient(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
	LRESULT onRemoveClient(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
	LRESULT onContextAction(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
	LRESULT onCustomDraw(int /*idCtrl*/, LPNMHDR pnmh, BOOL& bHandled);
	LRESULT onContextMenu(UINT /*uMsg*/, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT onReload(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/){
		update();
		return 0; // pothead add svn57
	}
	LRESULT onDblClick(int /*idCtrl*/, LPNMHDR /* pnmh */, BOOL& bHandled) {
		return onChangeClient(0, 0, 0, bHandled);
	}
	
	// Final message
	virtual void OnFinalMessage(HWND /*hWnd*/) 
	{
		delete this;
	}

	// Update control layouts
	void UpdateLayout(BOOL bResizeBars = TRUE);
protected:
	void addEntry(const ClientVersion& cv, int pos);
private:
	void update();
	void addLine(const string& aLine){
		ctrlStatus.SetWindowText(("*** " + aLine).c_str());
	}

	// Contained controls
	CStatusBarCtrl ctrlStatus;
	ExListViewCtrl ctrlVersions;
	CButton ctrlAdd, ctrlEdit, ctrlRemove, ctrlOpenXml, ctrlOpenSite, ctrlCheckVersion, ctrlReload;
	CContainedWindow listContainer;
	ClientVersion currentVersionProfile;

	bool closed;

	enum 
	{
		COLUMN_FIRST = 0,
		COLUMN_CLIENT_NAME = COLUMN_FIRST,
		COLUMN_LAST_VERSION,
		COLUMN_CURRENT_VERSION,
		COLUMN_COMMENT,
		COLUMN_ADDRESS,
		COLUMN_CLIENT_ADDRESS,
		COLUMN_UPDATE_TIME,
		COLUMN_ALERT,
		COLUMN_UPDATED,
		COLUMN_LAST
	};

	// Column parameters
	static int columnIndexes[];
	static int columnSizes[];

	virtual void on(VersionManagerListener::Types type) throw();
	virtual void on(UpdateManagerListener::Types type, int file) throw(){
		if( type == UpdateManagerListener::COMPLETE && file == UpdateManager::CLIENTVERSION ){
			update();
		}
	}
	string downBuf;
	//End
};

#endif

/**
 File end
 */